package com.smile.stream.stream;

import com.smile.stream.StreamUtil;
import com.smile.stream.pojo.Dish;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/7 16:32
 */
public class StreamDemo0 {
    /**
     * 筛选出卡路里小雨400的菜品，并按照卡路里排序
     *  java7和java8不同的实现方式
     * @param args
     */
    public static void main(String[] args) {
        //region java 7
        List<Dish> menu = StreamUtil.getMenu();
        List<Dish> lowCaloricDishes = new ArrayList<>();
        for (Dish d : menu) {
            if (d.getCalories() < 400) {
                lowCaloricDishes.add(d);
            }
        }
        //用累加器筛 选元素
        Collections.sort(lowCaloricDishes,new Comparator<Dish>() {
            @Override
            public int compare(Dish d1,Dish d2) {
                return Integer.compare(d1.getCalories(),d2.getCalories());
            }
        });
        List<String> lowCaloricDishesName = new ArrayList<>();
        for (Dish d : lowCaloricDishes) {
            lowCaloricDishesName.add(d.getName());
        }
        System.out.println(lowCaloricDishesName);
        //endregion

        System.out.println("##############");
        //region java8
        List<String> collect = menu.stream()
                .filter(d -> d.getCalories() < 400)
                .sorted(Comparator.comparing(Dish::getCalories))
                .map(Dish::getName)
                .collect(Collectors.toList());

        System.out.println(collect);
        //endregion

    }
}
